/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       orthogonalcamera.cpp
 * Author:     karooolek
 * Created on: 2009-04-03
 *
 **********************************************************************************************************************/

#include "orthogonalcamera.h"

namespace mGameEngine
{
namespace Graphics
{

OrthogonalCamera::OrthogonalCamera(float zoom,
    float ratio, bool autoRatio,
    float nearClip, float farClip) :
        Camera(ratio, autoRatio, nearClip, farClip), _zoom(zoom)
{
}

OrthogonalCamera::~OrthogonalCamera()
{
}

bool OrthogonalCamera::isInCamera(const Sphere &sphere) const
{
    const Position &pos = sphere.getCenter();
    float r = sphere.getRadius();

    // near/far plane clipping
    if(-pos.z < _nearClip - r || -pos.z > _farClip + r)
    {
        return false;
    }

    // top/bottom plane clipping
    if(abs(pos.y) >  0.5f / _zoom + r)
    {
        return false;
    }

    // left/right plane clipping
    float ratio = _ratio;

    if(_autoRatio)
    {
        int viewport[4];
        glGetIntegerv(GL_VIEWPORT, viewport);
        ratio = float(viewport[2]) / float(viewport[3]);
    }

    if(abs(pos.x) > 0.5f * ratio / _zoom + r)
    {
        return false;
    }

    return true;
}

float OrthogonalCamera::getProjectedSize(const Sphere &sphere) const
{
    float r = sphere.getRadius();
    return r * _zoom;
}

void OrthogonalCamera::use() const
{
    float ratio = _ratio;

    if(_autoRatio)
    {
        int viewport[4];
        glGetIntegerv(GL_VIEWPORT, viewport);
        ratio = float(viewport[2]) / float(viewport[3]);
    }

    float h = 0.5f / _zoom;
    float w = h * ratio;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w, w, -h, h, _nearClip, _farClip);
}

}
}

